package servlet;

import bean.Student;
import service.impl.StudentServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/getstudents")
public class GetStudents extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        String pageindex = req.getParameter("pageindex");//获得当前页码值
        int index = pageindex==null?1:Integer.parseInt(pageindex);
        StudentServiceimpl studentServiceimpl = new StudentServiceimpl();
        List<Student> students = studentServiceimpl.getstulist(stuname,stuno,sex==null|| sex.equals("")?-1:Integer.parseInt(sex),index,5);
        int total = studentServiceimpl.gettotal(stuname, stuno, sex == null || sex.equals("")? -1 : Integer.parseInt(sex));
        int sumpage = total%5>0?total/5+1:total/5;
        req.setAttribute("students",students);
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        req.setAttribute("index",index);
        req.setAttribute("total",total);
        req.setAttribute("sumpage",sumpage);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
